<template>
  <div class="container">
    <div class="app-container">
      <el-button
        type="primary"
        size="mini"
        style="margin:10px;"
        @click="handleAdd(0, 1)"
      >新增权限</el-button>

      <el-table :data="treeData" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              size="mini"
              @click="handleAdd(row.id, 2)"
            >新增</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleEdit(row.id)"
            >编辑</el-button>
            <el-popconfirm
              title="确定删除么?"
              class="el-pop"
              @onConfirm="handleDel(row.id)"
            >
              <template #reference>
                <el-button
                  type="text"
                  size="mini"
                >删除</el-button>
              </template>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/编辑 权限点弹框 -->
    <el-dialog
      :title="activeTitle"
      :visible.sync="showPermDialog"
      width="40%"
    >
      <el-form
        ref="permRef"
        :model="permForm"
        :rules="permRules"
        label-width="90px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permForm.name" size="mini" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permForm.code" size="mini" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permForm.description" size="mini" />
        </el-form-item>
        <el-form-item label="是否启用" prop="enVisible">
          <el-switch
            v-model="permForm.enVisible"
            :active-value="1"
            :inactive-value="0"
            size="mini"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="handleConfirm"
          >确定</el-button>
          <el-button size="mini" @click="handleCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  delPermAPI,
  getPermDetailAPI,
  getPermListAPI,
  postPermAPI,
  putPermAPI
} from '@/api/permission'
import { list2Tree } from '@/utils'

export default {
  name: 'Permisson',
  data() {
    return {
      // 权限点树形数据
      treeData: [],
      // 权限点表单
      permForm: {
        name: '',
        code: '',
        description: '',
        enVisible: 0
      },
      // 校验规则
      permRules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      // 控制权限弹框可见性
      showPermDialog: false,
      // 记录当前操作类型(新增或编辑)
      operType: 'add'
    }
  },
  computed: {
    // 记录编辑状态
    isEdit() {
      return this.operType === 'edit'
    },
    // 弹框标题
    activeTitle() {
      return this.isEdit ? '编辑权限点' : '新增权限点'
    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    // 删除
    async handleDel(id) {
      await delPermAPI(id)
      this.$message.success('删除权限点成功')
      this.getPermList()
    },
    // 新增/编辑 确定
    async handleConfirm() {
      try {
        // 整体校验
        await this.$refs.permRef.validate()
        if (this.isEdit) { // 编辑
          await putPermAPI(this.permForm)
          this.$message.success('修改权限点成功')
        } else { // 新增
          await postPermAPI({
            ...this.permForm,
            pid: this.pid,
            type: this.type
          })
          this.$message.success('新增权限点成功')
        }
        this.handleCancel()
        this.getPermList()
        // eslint-disable-next-line no-empty
      } catch (err) {
      }
    },
    // 取消
    handleCancel() {
      this.showPermDialog = false
      this.$refs.permRef.resetFields()
      if (this.isEdit) {
        Object.keys(this.permForm).forEach(key => {
          this.permForm[key] = ''
        })
      }
    },
    // 新增权限点弹框打开
    handleAdd(pid, type) {
      this.showPermDialog = true
      this.pid = pid
      this.type = type
      this.operType = 'add'
    },
    // 编辑权限点弹框打开
    async handleEdit(id) {
      this.showPermDialog = true
      this.operType = 'edit'
      this.permForm = await getPermDetailAPI(id)
    },
    // 获取权限点列表
    async getPermList() {
      // 请求权限列表
      const permList = await getPermListAPI()
      // 做树形转换并赋值
      this.treeData = list2Tree(permList, 0)
    }
  }
}
</script>

<style scoped>
.el-input {
  width: 95%;
}
.el-pop {
  margin-left: 10px;
}
</style>
